********************************************************************************
* This do file does summary statistics using 
*	- Baseline characteristics of affected firms and affected products
* Outputs: Table 1, Table 2, Table A1, Table A2
* Last modified by: Yukun Wang
* Date: 06/12/2017
********************************************************************************


clear all
set more off
set scheme s1color
set matsize 11000

***Set directory
capture cd "/Users/Dropbox/Chinese food exports"


********************
*  Table 1 Panel A *
********************

use "Data/Coded data/Customs/summstats_firm.dta", clear

keep if dairyfirm==1

replace avg_size=avg_size/10^6

//All firms
cap mat drop _all

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v',d
	mat S = nullmat(S)\ (int(r(N)) , round(r(p50),0.01), round(r(mean),0.01), round(r(sd),0.01))
}

mat list S

//By affected firms

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if affected_firms==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if innocent_firms==1,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if innocent_firms==0 & affected_firms==0,d
	mat S3 = nullmat(S3)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v of var avg_size avg_exp avg_oecd_share {
	reg `v' affected_firms if innocent_firms==0,r
	mat temp=r(table)
	testparm affected_firms
	mat S4 =nullmat(S4)\[round(temp[1,1],0.01),round(r(p),0.01)]\[round(temp[2,1],0.01),J(1,1,.)]
	}

mat S=S1,S2,S3,S4


tempname texfile
file open `texfile' using Results/main_tables/table_1_panel_a.tex, write replace
file close `texfile'

estout matrix(S) using Results/main_tables/table_1_panel_a.tex, label style(tex) collabels(none) eqlabels(none) append 



************************
*   Table A1 Panel A   *
************************

use "Data/Coded data/Customs/summstats_firm.dta", clear

replace avg_size=avg_size/10^6

keep if nondairyfoodfirm==1

//All firms
cap mat drop _all

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v',d
	mat S = nullmat(S)\ (int(r(N)) , round(r(p50),0.01), round(r(mean),0.01), round(r(sd),0.01))
}

mat list S

//By affected firms

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if affected_firms==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if innocent_firms==1,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if innocent_firms==0 & affected_firms==0,d
	mat S3 = nullmat(S3)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v of var avg_size avg_exp avg_oecd_share {
	reg `v' affected_firms if innocent_firms==0,r
	mat temp=r(table)
	testparm affected_firms
	mat S4 =nullmat(S4)\[round(temp[1,1],0.01),round(r(p),0.01)]\[round(temp[2,1],0.01),J(1,1,.)]
	}

mat S=S1,S2,S3,S4	

//mat S=S[.,1..3]


tempname texfile
file open `texfile' using Results/appendix_tables/table_a1_panel_a.tex, write replace
file close `texfile'

estout matrix(S) using Results/appendix_tables/table_a1_panel_a.tex, label style(tex) collabels(none) eqlabels(none) append 




*****************
*    Table 2    *
*****************

use "Data/Coded data/Customs/summstats_product.dta", clear

***dairy products

preserve

keep if dairy==1

//All products
cap mat drop _all

foreach v in avg_size avg_exp avg_oecd_share {
	sum `v',d
	mat S = nullmat(S`i')\ (int(r(N)) , round(r(p50),0.01), round(r(mean),0.01), round(r(sd),0.01))
}

mat list S

//By affected products

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if affected_products==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share{
	sum `v' if affected_products==0,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share{
	reg `v' affected_products,r
	mat temp=r(table)
	testparm affected_products
	mat S3 =nullmat(S3)\[round(temp[1,1],0.01),round(r(p),0.01)]\[round(temp[2,1],0.01),J(1,1,.)]
	}
mat S=S1,S2,S3	

//mat S=S[.,1..3]


tempname texfile
file open `texfile' using Results/main_tables/table_2_panel_a.tex, write replace
file close `texfile'

estout matrix(S) using Results/main_tables/table_2_panel_a.tex, label style(tex) collabels(none) eqlabels(none) append 

restore



***non-dairy food products

preserve

keep if dairy==0

//All products
cap mat drop _all

foreach v in avg_size avg_exp avg_oecd_share {
	sum `v',d
	mat S = nullmat(S`i')\ (int(r(N)) , round(r(p50),0.01), round(r(mean),0.01), round(r(sd),0.01))
}

mat list S

//By affected products

foreach v in avg_size avg_exp avg_oecd_share {
	sum `v' if affected_products==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share {
	sum `v' if affected_products==0,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.01)]\[J(1,1,.),round(r(sd),0.01)]
}

foreach v in avg_size avg_exp avg_oecd_share {
	reg `v' affected_products,r
	mat temp=r(table)
	testparm affected_products
	mat S3 =nullmat(S3)\[round(temp[1,1],0.01),round(r(p),0.01)]\[round(temp[2,1],0.01),J(1,1,.)]
	}
mat S=S1,S2,S3	

//mat S=S[.,1..3]


tempname texfile
file open `texfile' using Results/main_tables/table_2_panel_b.tex, write replace
file close `texfile'

estout matrix(S) using Results/main_tables/table_2_panel_b.tex, label style(tex) collabels(none) eqlabels(none) append 

restore



***********************
*   Table 1 Panel B   *
***********************


***load data

use "Data/Coded data/manucensus_coded",clear

***Summary statistic by affected firms within dairy: baseline characteristics
preserve 

keep if dairy==1
keep if year>=2005 & year<2008		

collapse (mean) employment salesrevenue lnemployment lnsalesrevenue affected innocent, by(panelid)

set more off
cap mat drop _all

foreach v in employment lnemployment salesrevenue lnsalesrevenue {
	sum `v' if affected==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v in employment lnemployment salesrevenue lnsalesrevenue {
	sum `v' if innocent==1,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v in employment lnemployment salesrevenue lnsalesrevenue {
	sum `v' if innocent==0 & affected==0,d
	mat S3 = nullmat(S3)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v of var employment lnemployment salesrevenue lnsalesrevenue {
	reg `v' affected if innocent==0,r
	mat temp=r(table)
	testparm affected
	mat S4 =nullmat(S4)\[round(temp[1,1],0.001),round(r(p),0.001)]\[round(temp[2,1],0.001),J(1,1,.)]
	}

mat S=S1,S2,S3,S4	


tempname texfile
file open `texfile' using Results/main_tables/table_1_panel_b.tex, write replace
file close `texfile'

estout matrix(S) using Results/main_tables/table_1_panel_b.tex, label style(tex) collabels(none) eqlabels(none) append 


restore


***********************
*   Table A1 Panel B  *
***********************

***Summary statistic by affected firms within non-dairy food: baseline characteristics

preserve 

keep if dairy==0 & food==1
keep if year>=2005 & year<2008

collapse (mean) employment salesrevenue lnemployment lnsalesrevenue affected innocent, by(panelid private)

set more off
cap mat drop _all

foreach v in employment lnemployment salesrevenue lnsalesrevenue {
	sum `v' if affected==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v in employment lnemployment salesrevenue lnsalesrevenue {
	sum `v' if innocent==1,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v in employment lnemployment salesrevenue lnsalesrevenue {
	sum `v' if innocent==0 & affected==0,d
	mat S3 = nullmat(S3)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v of var employment lnemployment salesrevenue lnsalesrevenue {
	reg `v' affected if innocent==0,r
	mat temp=r(table)
	testparm affected
	mat S4 =nullmat(S4)\[round(temp[1,1],0.001),round(r(p),0.001)]\[round(temp[2,1],0.001),J(1,1,.)]
	}

mat S=S1,S2,S3,S4	



tempname texfile
file open `texfile' using Results/appendix_tables/table_a1_panel_b.tex, write replace
file close `texfile'

estout matrix(S) using Results/appendix_tables/table_a1_panel_b.tex, label style(tex) collabels(none) eqlabels(none) append 


restore


***********************
*       Table A2      *
***********************

***Panel A***

***load data
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_dairy_dropban0.dta", clear

keep if year==2007
drop if interm==1

label var round2 "2nd round of inspection dummy"


keep if hs_id=="04021000" | hs_id=="04022100" | hs_id=="04022900"

collapse (sum) value quantity oecd_value oecd_quantity (mean) price oecd_price firm_sales_bl firm_exp_bl experienced round2, by (party_id)



foreach v in value oecd_value quantity oecd_quantity price oecd_price firm_sales_bl firm_exp_bl experienced{
	sum `v' if round2==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}


foreach v in value oecd_value quantity oecd_quantity price oecd_price firm_sales_bl firm_exp_bl experienced{
	sum `v' if round2==0,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

*Compare
foreach v of var value oecd_value quantity oecd_quantity price oecd_price firm_sales_bl firm_exp_bl experienced{
	reg `v' round2, r
	mat temp=r(table)
	testparm round2
	mat S3 =nullmat(S3)\[round(temp[1,1],0.001),round(r(p),0.001)]\[round(temp[2,1],0.001),J(1,1,.)]
	}

mat S=S1,S2,S3



tempname texfile
file open `texfile' using Results/appendix_tables/table_a2_panel_a.tex, write replace
file close `texfile'

estout matrix(S) using Results/appendix_tables/table_a2_panel_a.tex, label style(tex) collabels(none) eqlabels(none) append


***Panel B***
***Manucensus
use "Data/Coded data/manucensus_coded",clear
keep if year==2007
keep if dairy==1

cap mat drop _all

foreach v in private employment lnemployment salesrevenue lnsalesrevenue{
	sum `v' if round2==1,d
	mat S1 = nullmat(S1)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v in private employment lnemployment salesrevenue lnsalesrevenue{
	sum `v' if round2==0,d
	mat S2 = nullmat(S2)\[r(N),round(r(mean),0.001)]\[J(1,1,.),round(r(sd),0.001)]
}

foreach v of var private employment lnemployment salesrevenue lnsalesrevenue{
	reg `v' round2, r
	mat temp=r(table)
	testparm round2
	mat S3 =nullmat(S3)\[round(temp[1,1],0.001),round(r(p),0.001)]\[round(temp[2,1],0.001),J(1,1,.)]
	}

mat S=S1,S2,S3	



tempname texfile
file open `texfile' using Results/appendix_tables/table_a2_panel_b.tex, write replace
file close `texfile'

estout matrix(S) using Results/appendix_tables/table_a2_panel_b.tex, label style(tex) collabels(none) eqlabels(none) append 



********************************
*           Table A5:          *
*          Cell Count          *
********************************

***Reload data
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_step1.dta", clear
keep if food==1   //keep only food
drop if year==2008  //drop 2008
gen post=(year>=2009)
egen partyyearid=group(party_id year)
collapse (mean) affected_firms affected_products dairy post, by(partyyearid hs_id)
count if dairy==1 & affected_firms==1 & affected_products==1 & post==0
count if dairy==0 & affected_firms==1 & affected_products==1 & post==0
count if dairy==1 & affected_firms==1 & affected_products==0 & post==0
count if dairy==0 & affected_firms==1 & affected_products==0 & post==0

count if dairy==1 & affected_firms==0 & affected_products==1 & post==0
count if dairy==0 & affected_firms==0 & affected_products==1 & post==0
count if dairy==1 & affected_firms==0 & affected_products==0 & post==0
count if dairy==0 & affected_firms==0 & affected_products==0 & post==0

count if dairy==1 & affected_firms==1 & affected_products==1 & post==1
count if dairy==0 & affected_firms==1 & affected_products==1 & post==1
count if dairy==1 & affected_firms==1 & affected_products==0 & post==1
count if dairy==0 & affected_firms==1 & affected_products==0 & post==1

count if dairy==1 & affected_firms==0 & affected_products==1 & post==1
count if dairy==0 & affected_firms==0 & affected_products==1 & post==1
count if dairy==1 & affected_firms==0 & affected_products==0 & post==1
count if dairy==0 & affected_firms==0 & affected_products==0 & post==1

***Reload data and drop banned destinations
use "Data/Coded data/Customs/customs_yearly_2000_2013_firmproduct_level_step1.dta", clear
keep if food==1   //keep only food
drop if year==2008  //drop 2008
merge m:1 origin_id using "Data/Codebooks/ban_country_list.dta"
drop if _m==2
drop _m
drop if ban_country==1
gen post=(year>=2009)
egen partyyearid=group(party_id year)
collapse (mean) affected_firms affected_products dairy post, by(party_id year hs_id)
count if dairy==1 & affected_firms==1 & affected_products==1 & post==0
count if dairy==0 & affected_firms==1 & affected_products==1 & post==0
count if dairy==1 & affected_firms==1 & affected_products==0 & post==0
count if dairy==0 & affected_firms==1 & affected_products==0 & post==0

count if dairy==1 & affected_firms==0 & affected_products==1 & post==0
count if dairy==0 & affected_firms==0 & affected_products==1 & post==0
count if dairy==1 & affected_firms==0 & affected_products==0 & post==0
count if dairy==0 & affected_firms==0 & affected_products==0 & post==0

count if dairy==1 & affected_firms==1 & affected_products==1 & post==1
count if dairy==0 & affected_firms==1 & affected_products==1 & post==1
count if dairy==1 & affected_firms==1 & affected_products==0 & post==1
count if dairy==0 & affected_firms==1 & affected_products==0 & post==1

count if dairy==1 & affected_firms==0 & affected_products==1 & post==1
count if dairy==0 & affected_firms==0 & affected_products==1 & post==1
count if dairy==1 & affected_firms==0 & affected_products==0 & post==1
count if dairy==0 & affected_firms==0 & affected_products==0 & post==1

